﻿using MSL.Tool;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using TimedTask.Models;

//----------------------------------------------------------------*/
// 版权所有：
// 文 件 名：TimedTaskNotifyIcon.cs
// 功能描述：系统托盘类
// 创建标识：m.sh.lin0328@163.com 2014/9/2 20:53:59
// 修改描述：
//----------------------------------------------------------------*/
namespace TimedTask
{
    public class NotifyIconHelper
    {
        private static NotifyIconHelper instance = null;
        public static NotifyIcon NotifyIcon = new NotifyIcon();

        #region 单例

        private NotifyIconHelper()
        {
            SetupNotifyIcon();
        }
        public static NotifyIconHelper Instance()
        {
            if (instance == null)
            {
                instance = new NotifyIconHelper();
            }
            return instance;
        }
        #endregion

        #region 提示信息

        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        public void ShowBalloonTip(string title, string text)
        {
            if (String.IsNullOrEmpty(text))
                return;
            try
            {
                NotifyIcon.BalloonTipTitle = title;
                NotifyIcon.BalloonTipText = text;
                NotifyIcon.BalloonTipIcon = ToolTipIcon.None;

                NotifyIcon.ShowBalloonTip(5000);
            }
            catch (Exception ex)
            {
                LogHelper.SaveErrorLog("ShowBalloonTip BalloonTipTitle:[" + title + "]\r\nBalloonTipText:[" + text + "]", ex);
            }
        }
        #endregion

        #region 托盘菜单

        /// <summary>
        /// 托盘菜单
        /// </summary>
        private void SetupNotifyIcon()
        {
            NotifyIcon.Icon = Properties.Resources.App;
            NotifyIcon.ContextMenuStrip = new ContextMenuStrip();
            
            NotifyIcon.ContextMenuStrip.Items.AddRange(new ToolStripItem[] {
                                     new ToolStripMenuItem("访问主页", Properties.Resources.Home,new EventHandler((s,e)=>{
                                          System.Diagnostics.Process.Start(Models.Const.HomePageUrl);
                                     })),
                                     new ToolStripMenuItem("关于作者", Properties.Resources.About, new EventHandler((s,e)=>{
                                         new Views.About().Show();
                                     })),
                                     new ToolStripSeparator(),
                                     new ToolStripMenuItem("工具集", Properties.Resources.Tools,
                                           new ToolStripItem[]{
                                                 new ToolStripMenuItem("文件下载", Properties.Resources.ImgDown, new EventHandler((s,e)=>{
                                                     new Views.PageFileDown().Show();
                                                 })),
                                                 new ToolStripMenuItem("正则测试", Properties.Resources.Reg, new EventHandler((s,e)=>{
                                                     Helper.StartApp(Const.StartPath + "\\Tools\\reg.exe");
                                                 })),
                                                 new ToolStripMenuItem("图片压缩", Properties.Resources.ImgCompress, new EventHandler((s,e)=>{
                                                     Helper.StartApp(Const.StartPath + "\\Tools\\img.exe");
                                                 })),
                                                 new ToolStripMenuItem("虚拟光驱", Properties.Resources.CD, new EventHandler((s,e)=>{
                                                     Helper.StartApp(Const.StartPath + "\\Tools\\virtualdrivemaster.exe");
                                                 }))
                                          }
                                     ),
                                     new ToolStripSeparator(),
                                     new ToolStripMenuItem("退出", Properties.Resources.Exit, new EventHandler(ExitMenuItem_Click)) 
                            });
            NotifyIcon.Visible = true;
        }
        #endregion
        
        // 退出
        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            Helper.AlertConfirm(null, "确定要退出托盘应用程序吗？", () =>
            {
                Helper.ExitApp();
                NotifyIconHelper.Instance().Hide();
            });
        }
        // 隐藏
        public void Hide()
        {
            NotifyIcon.Visible = false;
        }
    }
}
